package in.abacritt.android.xclock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class XClockBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		boolean updateWeather = false;

		XClockWidgetUpdater xClockWidgetUpdater = new XClockWidgetUpdater(
				context);

		if (intent.getAction() != null) {
			Log.d("broadcast", intent.getAction());
			if (intent.getAction().equalsIgnoreCase(
					XClockWidgetProvider.MY_WIDGET_WEATHER_UPDATE_COMP)) {

				WeatherInfo weatherInfo = (WeatherInfo) intent
						.getSerializableExtra(XClockWidgetProvider.MY_WEATHER_INFO);

				if (weatherInfo != null) {
					updateWeather = true;
					xClockWidgetUpdater.setWeatherInfo(weatherInfo);
				}

			} else if (intent.getAction().equalsIgnoreCase(
					XClockWidgetProvider.MY_WIDGET_WEATHER_UPDATE)) {
				Intent msgIntent = new Intent(context,
						OpenWeatherUpdateService.class);
				context.getApplicationContext().startService(msgIntent);
			}
		}

		xClockWidgetUpdater.runAndUpdate(updateWeather);
	}

}
